package light.head.controller.inv;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.config.ConfigParm;
import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Url;
import light.head.dao.inv.InvCommodityInfoDAO;
import light.head.dao.inv.InvCommodityItemDAO;
import light.head.dao.inv.InvUnitDAO;
import light.head.dao.sys.OrganDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.model.inv.InvCommodityInfo;
import light.head.model.inv.InvCommodityItem;
import light.head.model.inv.InvUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 商品信息
 * @author yuleidian
 */
@Controller
@RequestMapping(Url.INVCOMMODITYINFO)
public class InvCommodityInfoController extends SupporterController{
	private InvCommodityInfoDAO ciDAO;
	private InvCommodityItemDAO cItemDAO;
	private OrganDAO organDAO;
	private InvUnitDAO iuDAO;
	
	@RequestMapping(Url.INIT)
	public ModelAndView tree(HttpServletRequest request, HttpServletResponse response) {
		Integer oc =organDAO.getRoot(cu_organCode(request)).getCode();
		List<InvCommodityInfo> list = ciDAO.getAll(oc);
		if (null == list) {
			return new ModelAndView(Url.INVCOMMODITYINFO_TREE);	
		}
		StringBuffer tree = new StringBuffer();
		for (InvCommodityInfo ci : list) {
			if (null != ci) {
				tree.append("<ul>" +
						"<li><span  onmouseover='display_InvCommodityInfo_in($(this));' onmouseout='display_InvCommodityInfo_out($(this));'><i class='icon-calendar'></i>&nbsp;" +ci.getName() +
								"<em  class='display' style='display:none;' >&nbsp;&nbsp;<a style=\"margin-left:40px;\" href=\"javascript:edit_InvCommodityInfo('" + ci.getCode() + "');\" \" title=\"编辑详细信息\"><img src=\""+ConfigParm.getContextPath()+"images/tree/img_614.gif\" /></a>"+
								"&nbsp;&nbsp;<a style=\"margin-left:5px;\" href=\"javascript:edit_InvCommodityInfo('" + ci.getCode() + "','" + ci.getCode() + "');\" title=\"添加子项\"><img src=\""+ConfigParm.getContextPath()+"images/tree/img_12.gif\" / ></a>"+
								"&nbsp;&nbsp;<a style=\"margin-left:5px;\" href=\"javascript:delete_InvCommodityInfo('" + ci.getCode() + "');\" title=\"删除该项\"><img src=\""+ConfigParm.getContextPath()+"images/tree/img_613.gif\" / ></a></em></span>");
				addree(tree, ci.getSonList());
				tree.append("</li></ul>");
			}
		}
		request.setAttribute(Parm.TREE, tree.toString());
		return new ModelAndView(Url.INVCOMMODITYINFO_TREE);	
	}	
	private void addree(StringBuffer tree, List<InvCommodityInfo> list) {
		if (null == tree || null == list || list.size() == 0)
			return;
		for (InvCommodityInfo ci : list) {
			if (null != ci) {
				tree.append("<ul><li><span  onmouseover='display_InvCommodityInfo_in($(this));' onmouseout='display_InvCommodityInfo_out($(this));'><i class='icon-plus-sign'></i>&nbsp;" +ci.getName() +
								"<em class='display' style='display:none;' onmouseover='display_InvCommodityInfo($(this);'>&nbsp;&nbsp;<a style=\"margin-left:40px;\" href=\"javascript:edit_InvCommodityInfo('" + ci.getCode() + "',null);\" title=\"编辑详细信息\"><img src=\""+ConfigParm.getContextPath()+"images/tree/img_614.gif\" /></a>"+
								"&nbsp;&nbsp;<a style=\"margin-left:5px;\" href=\"javascript:edit_InvCommodityInfo('" + ci.getCode() + "','" + ci.getCode() + "');\" title=\"添加子项\"><img src=\""+ConfigParm.getContextPath()+"images/tree/img_12.gif\" /></a>"+
								"&nbsp;&nbsp;<a style=\"margin-left:5px;\" href=\"javascript:delete_InvCommodityInfo('" + ci.getCode() + "',null);\" title=\"删除该项\"><img src=\""+ConfigParm.getContextPath()+"images/tree/img_613.gif\" /></a></em></span>");
				addree(tree, ci.getSonList());
				tree.append("</li></ul>");
			}
		}
	}
	
	@RequestMapping(value = Url.EDIT)
	public ModelAndView edit(Integer code, Integer pc,HttpServletRequest request, HttpServletResponse response) throws Exception {
		if(code != null){
			InvCommodityInfo info = ciDAO.get(code);
			if(info != null){
				info.setcItem(cItemDAO.getCommodityItemByCc(info.getCode(), info.getOc()));
				request.setAttribute(Parm.INFO, info);
			}
		}
		if(pc != null){
			request.setAttribute(Parm.PC, pc);
		}
		return new ModelAndView(Url.INVCOMMODITYINFO_EDIT);
	}
	
	
	@RequestMapping(value = Url.SAVE)
	public void save(InvCommodityInfo ci,HttpServletRequest request, HttpServletResponse response) throws Exception {
		if (ci == null) {
			print(response, Msg.PARAMETER_ERROR);
			return ;
		}
		RESULT result = RESULT.ERROR;
		//更新
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		if (ci.getCode() != null) {
			InvCommodityInfo temp = ciDAO.get(ci.getCode());
			if(temp != null){
				ci.setDeleted(temp.getDeleted());
				if(ci.getIsCommodity() == temp.getIsCommodity()){
					result = ciDAO.upd(ci);
					if(result == RESULT.SUCCESS){
						if(ci.getIsCommodity() == true){
							List<InvUnit> typeList = iuDAO.findAll(oc,ci.getcItem().getUn());
							if (null != typeList && typeList.size() > 0) {
								ci.getcItem().setUc(typeList.get(0).getCode());
							} else {
								InvUnit i = new InvUnit();
								i.setCt(new Date());
								i.setCu(cu_code(request));
								i.setDeleted(false);
								i.setName(ci.getcItem().getUn());
								i.setOc(cu_organCode(request));
								iuDAO.add(i);
								ci.getcItem().setUc(i.getCode());
							}
							ci.getcItem().setDeleted(false);
							ci.getcItem().setName(ci.getName());
							result = cItemDAO.upd(ci.getcItem());
						}						
					}
				} else {
					if(ci.getIsCommodity() == true){
						List<InvUnit> typeList = iuDAO.findAll(oc,ci.getcItem().getUn());
						if (null != typeList && typeList.size() > 0) {
							ci.getcItem().setUc(typeList.get(0).getCode());
						} else {
							InvUnit i = new InvUnit();
							i.setCt(new Date());
							i.setCu(cu_code(request));
							i.setDeleted(false);
							i.setName(ci.getcItem().getUn());
							i.setOc(cu_organCode(request));
							iuDAO.add(i);
							ci.getcItem().setUc(i.getCode());
						}
						result = ciDAO.upd(ci);
						ci.getcItem().setDeleted(false);
						ci.getcItem().setCc(ci.getCode());
						ci.getcItem().setName(ci.getName());
						ci.getcItem().setOc(organDAO.getRoot(cu_organCode(request)).getCode());
						result = cItemDAO.add(ci.getcItem());
					} else {
						result = ciDAO.upd(ci);
						ci.getcItem().setDeleted(true);
						result = cItemDAO.update(ci.getcItem());
					}
				}
			}
		} else {
		//添加
			//保存商品信息表
			ci.setCt(new Date());
			ci.setOc(organDAO.getRoot(cu_organCode(request)).getCode());
			ci.setCu(cu_code(request));
			ci.setDeleted(false);
			result = ciDAO.add(ci);
			if(result == RESULT.ERROR){
				print(response, Msg.ERROR);
				return;
			}
			//保存商品信息详情表
			//添加
			if(ci.getcItem().getCode() == null && ci.getIsCommodity() == true){
				
				List<InvUnit> typeList = iuDAO.findAll(oc,ci.getcItem().getUn());
				if (null != typeList && typeList.size() > 0) {
					ci.getcItem().setUc(typeList.get(0).getCode());
				} else {
					InvUnit i = new InvUnit();
					i.setCt(new Date());
					i.setCu(cu_code(request));
					i.setDeleted(false);
					i.setName(ci.getcItem().getUn());
					i.setOc(cu_organCode(request));
					iuDAO.add(i);
					ci.getcItem().setUc(i.getCode());
				}
				ci.getcItem().setCc(ci.getCode());
				ci.getcItem().setName(ci.getName());
				ci.getcItem().setDeleted(false);
				ci.getcItem().setCc(ci.getCode());
				ci.getcItem().setOc(organDAO.getRoot(cu_organCode(request)).getCode());
				result = cItemDAO.add(ci.getcItem());
			}
			
		}
		if (RESULT.SUCCESS == result) {
			ciDAO.loadAll(cu_organCode(request));
			print(response, Msg.SUCCESS);
		} else {
			print(response, Msg.ERROR);
		}
	}
	
	@RequestMapping(value = Url.DELETE)
	public void delete(Integer code,HttpServletRequest request, HttpServletResponse response) throws Exception {
		RESULT result = RESULT.ERROR;
		if(code != null){
			InvCommodityInfo ci = ciDAO.get(code);
			if (ci != null) {
				ci.setDeleted(true);
				result = ciDAO.upd(ci);
				InvCommodityItem item = cItemDAO.getCommodityItemByCc(ci.getCode(),organDAO.getRoot(cu_organCode(request)).getCode());
				if(item != null && item.getCc() != null){
					item.setDeleted(true);
					result = cItemDAO.upd(item);
				}
			} 
		}
		if (RESULT.SUCCESS == result) {
			ciDAO.loadAll(cu_organCode(request));
			print(response, Msg.SUCCESS);
		} else {
			print(response, Msg.ERROR);
		}
	}
	
	
	@Autowired
	public void setCiDAO(InvCommodityInfoDAO ciDAO) {
		this.ciDAO = ciDAO;
	}
	@Autowired
	public void setcItemDAO(InvCommodityItemDAO cItemDAO) {
		this.cItemDAO = cItemDAO;
	}
	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}
	@Autowired
	public void setIuDAO(InvUnitDAO iuDAO) {
		this.iuDAO = iuDAO;
	}
	
}
